package com.viknix.jdbc;

import java.sql.*;
import java.util.Objects;

/**
 * @Author: Dongqi
 * @Date: 2021/11/4 10:03
 * @Version 1.0
 * @Description:
 */
public class JdbcConnMysql {

    public static void main(String[] args) throws SQLException {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://101.34.50.82:3306/thread-pool-manager", "root", "123456");
            String sql = "select * from thread_pool_info";
            ps = conn.prepareStatement(sql);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                // 可以通过列号来获取，JDBC当中所有下表从1开始
                System.out.println(resultSet.getString(1));
                // 也可以通过查询结果集的列名称，如果列重命名，必须是重命名之后的列明
                System.out.println(resultSet.getString("id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(resultSet)) {
                resultSet.close();
            }
            if (Objects.nonNull(ps)) {
                ps.close();
            }
            if (Objects.nonNull(conn)) {
                conn.close();
            }
        }
    }
}
